#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define PRINT( x ) printf( # x ": %d\n", x )

void check_isatty( int fd )
{
	if( isatty( fd ) ) {
		char buf[256];
		if( ttyname_r( fd, buf, 256 ) != 0 ) {
			memcpy( buf, "NULL", strlen( "NULL" ) + 1 );
		}
		printf( "fd %d is a tty. tty name %s\n", fd, buf );
	}
	else {
		printf( "fd %d is not a tty.\n", fd );
	}
}

int main( int argc, char *argv[] )
{
	if( argc > 1 ) {
		for( int i = 1; i < argc; i++ )
			check_isatty( atoi( argv[i] ) );
		return 0;
	}

	check_isatty( -1 );
	check_isatty( 0 );
	check_isatty( 1 );
	check_isatty( 2 );
	check_isatty( 3 );
	return 0;
}

